package com.maxuxu.scihome.controller.employee;

import com.maxuxu.scihome.pojo.Employee;
import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @className: EmployeeController
 * @author: Maxuxu
 * @date: 2022/7/5 15:16
 * @description:
 */
@RestController
@Slf4j
public class EmployeeController {
    @Autowired
    Employee employee;
    @Autowired
    Message message;
    @Autowired
    EmployeeService employeeService;

    public Employee getinfo(HttpServletRequest request){
        String name = request.getParameter("name");
        String cardId = request.getParameter("cardId");
        try {
            int sex = Integer.parseInt(request.getParameter("sex"));
            int jobId = Integer.parseInt(request.getParameter("job_id"));
            employee.setSex(sex);
            employee.setJobId(jobId);
        }catch (Exception e){
            System.out.println("没有输入性别和工号");
        }
        String birthday = request.getParameter("birthday");
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = ft.parse(birthday);
            employee.setBirthday(parse);
        } catch (ParseException e) {
            System.out.println("没有输入生日");
        }
        String education = request.getParameter("education");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String tel = request.getParameter("tel");
        String party = request.getParameter("party");
        String qqNum = request.getParameter("qqNum");
        String address = request.getParameter("address");
        String postCode = request.getParameter("postCode");

        String race = request.getParameter("race");
        String speciality = request.getParameter("speciality");
        String hobby = request.getParameter("hobby");
        String remark = request.getParameter("remark");
        int deptId = Integer.parseInt(request.getParameter("dept_id"));
        employee.setPostCode(postCode);
        employee.setRace(race);
        employee.setSpeciality(speciality);
        employee.setHobby(hobby);
        employee.setName(name);
        employee.setRemark(remark);
        employee.setAddress(address);
        employee.setCardId(cardId);
        employee.setDeptId(deptId);

        employee.setEducation(education);
        employee.setEmail(email);
        employee.setPhone(phone);
        employee.setTel(tel);
        employee.setParty(party);
        employee.setQqNum(qqNum);
        return employee;
    }

    @PostMapping("/employee/insert")
    public Message insert(HttpServletRequest request) throws ParseException {
        Employee getinfo = getinfo(request);
        log.error(String.valueOf(getinfo));
        employeeService.insertEmployee(getinfo);
        message.setJson("插入成功");
        return message;
    }

    @PostMapping("/employee/select")
    public Pages findAll(long current, long size) {
        return employeeService.selectPage(current, size);
    }


    @GetMapping("/employee/removeEmployee")
    public Message removedepart(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
                split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        employeeService.deletEmployee(list);
        message.setJson("删除成功"+ids);
        return message;
    }

    @GetMapping("/employee/selectone")
    public Employee findone(HttpServletRequest request){
        String name = request.getParameter("name");
        Employee employee1 = new Employee();
        employee1.setName(name);
        return employeeService.selectByAny(employee1);
    }

    @PostMapping("/employee/update")
    public Employee update(String old,HttpServletRequest request) throws ParseException {
        Employee getinfo = getinfo(request);
        log.error(old);
        employeeService.updateEmployee(old, getinfo);
        return getinfo;
    }
    
    
    
}
